'use strict';

const BaseService = require('./base');

const svgCaptcha = require('svg-captcha');
const sd = require('silly-datetime');
const md5 = require('md5');


class ToolsService extends BaseService {
    //生成验证码
    async captcha(width, height, background) {
        width = width ? width : 100;
        height = height ? height : 32;
        background = background ? background : "#cc9966";
        let captcha = svgCaptcha.create({
            size: 4,
            fontSize: 50,
            width,
            height,
            background
        });
        return captcha;
    }

    async md5(str) {
        return md5(str);
    }

    async getTime() {
        let d = new Date();
        return d.getTime();
    }

    async getDay() {
        let day = sd.format(new Date(), 'YYYYMMDD');
        return day;
    }

    async getRandomNum() {
        let random_str = '';
        for (let i = 0; i < 4; i++) {
            random_str += Math.floor(Math.random() * 10);
        }
        return random_str;
    }


}

module.exports = ToolsService;
